package com.wrox.todolist;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TodoItem implements Serializable {
	private static final long serialVersionUID = 1L;

	private int rowId;
	private String task;
	private Date created;
	private boolean finished = false;	
	
//	public TodoItem(String task) {
//		super();
//		this.task = task;
//		this.created = new Date(System.currentTimeMillis());
//	}
//
//	public TodoItem(String task, Date created, boolean finished) {
//		super();
//		this.task = task;
//		this.created = created;
//		this.finished = finished;
//	}

	public TodoItem(int rowId, String task, Date created, boolean finished) {
		super();
		this.rowId = rowId;
		this.task = task;
		this.created = created;
		this.finished = finished;
	}

	public boolean isFinished() {
		return finished;
	}


	public void setFinished(boolean finished) {
		this.finished = finished;
	}

	public String getTask() {
		return task;
	}

	public Date getCreated() {
		return created;
	}

	@Override
	public String toString() {		
		return (isFinished() ? "done " : "") + task + " " + getCreatedString();
	}

	public String getCreatedString() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
		return sdf.format(created);
	}

	private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException { 
		//always perform the default de-serialization first 
		aInputStream.defaultReadObject(); 
		//make defensive copy of the mutable Date field 
		created = new Date( created.getTime() ); 
	} 
	
	private void writeObject(ObjectOutputStream aOutputStream) throws IOException { 
		//perform the default serialization for all non-transient, non-static fields 
		aOutputStream.defaultWriteObject(); 
	}

	public int getId() {
		return rowId;
	}
}
